<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="handleSearch" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/addEnterprise')">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="getList" @expand-change="handleExpandChange">
        <el-table-column label="序号" type="index" align="center" width="80" />
        <el-table-column type="expand">
          <template #default="dataTable">
            <el-table :data="dataTable.row.expandList" style="width: 100%">
              <el-table-column label="租赁楼宇" width="320" prop="buildingName" />
              <el-table-column label="租赁起始时间" prop="startTime" />
              <el-table-column label="合同状态" prop="status" :formatter="statusFormatter" />
              <el-table-column label="操作" width="180">
                <template #default="{row}">
                  <el-button size="mini" type="text" :disabled="row.status === 3" @click="handleRetreat(row.id)">退租</el-button>
                  <el-button size="mini" type="text" :disabled="row.status === 1" @click="handleDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>

        </el-table-column>
        <el-table-column label="企业名称" width="320" prop="name" />
        <el-table-column label="联系人" prop="contact" />
        <el-table-column
          label="联系电话"
          prop="contactNumber"
        />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="mini" type="text" @click="handleAdd(row.id)">添加合同</el-button>
            <el-button size="mini" type="text" @click="handleDetail(row.id)">查看</el-button>
            <el-button size="mini" type="text" @click="handleEdit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="enterpriseChange"
      />
    </div>
    <AdditionContract :is-visible.sync="dialogTableVisible" :data-id="dataID" :get-enterprise-list="getEnterpriseList" />
  </div>
</template>
<script>
import AdditionContract from './AdditionContract.vue'
import { getEnterpriseListAPI, deleteEnterpriseAPI, getEnterpriseDetailAPI, getEnterpriseContractAPI, deleteEnterpriseContractAPI } from '@/api/enterprise'

export default {
  components: {
    AdditionContract
  },
  data() {
    return {
      // 添加合同id
      dataID: null,
      // 显示添加合同弹窗
      dialogTableVisible: false,
      // 获取展开的列表,渲染列表
      getList: [],
      params: {
        name: '',
        page: 1,
        pageSize: 2
      },
      total: 0

    }
  },
  async mounted() {
    // 获取企业列表
    await this.getEnterpriseList()
  },
  methods: {
    // 封装企业列表接口
    async getEnterpriseList() {
      const res = await getEnterpriseListAPI(this.params)
      // 把企业列表赋值给getList，并map映射数组添加展开列表，初始为空数组
      this.getList = res.data.rows.map((item) => {
        return {
          ...item,
          expandList: []
        }
      })
      this.total = res.data.total
    },
    // 分页，切换页码
    enterpriseChange(value) {
      // 把当前页码赋值给params
      this.params.page = value
      // 重新获取企业列表
      this.getEnterpriseList()
    },
    // 查询企业
    handleSearch() {
      this.getEnterpriseList()
    },
    // 编辑企业,通过id获取企业信息
    handleEdit(id) {
      this.$router.push({
        path: '/addEnterprise',
        query: {
          id
        }
      })
    },
    // 删除企业
    async handleDelete(id) {
      // 调用删除接口
      await deleteEnterpriseAPI(id)
      // 提示
      this.$message.success('删除成功')
      // 重新获取企业列表
      this.getEnterpriseList()
    },
    // 获取展开的列表
    async  handleExpandChange(row, rows) {
      // 判断是否展开,是否ID相同
      const isEnter = rows.find(item => item.id === row.id)
      // ID相同，展开
      if (isEnter) {
        const res = await getEnterpriseDetailAPI(row.id)
        // eslint-disable-next-line require-atomic-updates
        row.expandList = res.data
        // 添加到展开列表中
      }
    },
    // 格式化合同状态
    statusFormatter(row) {
      const MAP = {
        0: '待生效',
        1: '生效中',
        2: '已到期',
        3: '已退租'
      }
      return MAP[row.status]
    },
    // 退租
    async handleRetreat(id) {
      // 调用退租接口
      await getEnterpriseContractAPI(id)
      // 提示
      this.$message.success('退租成功')
      // 重新获取企业列表
      this.getEnterpriseList()
    },
    // 删除合同
    async handleDel(id) {
      await deleteEnterpriseContractAPI(id)
      this.$message.success('删除成功')
      this.getEnterpriseList()
    },
    // 添加合同
    handleAdd(id) {
      this.dialogTableVisible = true
      this.dataID = id
    },
    // 查看合同
    handleDetail(id) {
      this.$router.push({
        path: '/ViewBusiness',
        query: {
          id
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
